﻿using System;
using System.ComponentModel;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using System.Globalization;
using System.Linq;

namespace Presentation.ABDI.Intranet.VWPDestaqueAniversariante
{
    [ToolboxItemAttribute(false)]
    public class VWPDestaqueAniversariante : System.Web.UI.WebControls.WebParts.WebPart
    {
        // Visual Studio might automatically update this path when you change the Visual Web Part project item.
        private const string _ascxPath = @"~/_CONTROLTEMPLATES/Presentation.ABDI.Intranet/VWPDestaqueAniversariante/VWPDestaqueAniversarianteUserControl.ascx";

        protected override void CreateChildControls()
        {
            Control control = Page.LoadControl(_ascxPath);
            Controls.Add(control);
        }

        /// <summary>
        /// Renderizar página.
        /// </summary>
        /// <param name="writer"></param>
        protected override void RenderContents(HtmlTextWriter writer)
        {
            try
            {
                DateTime agora = DateTime.Now;

                SPSite mySite = SPContext.Current.Site;
                SPWeb myWeb = mySite.OpenWeb();
                SPList myUsuarios = myWeb.Lists["Usuário"];

                CultureInfo mesExtension = new CultureInfo("pt-BR");
                DateTimeFormatInfo dtif = mesExtension.DateTimeFormat;

                for (int mes = 1; mes <= 12; mes++)
                {
                    var resultUsuario = myUsuarios.Items.Cast<SPListItem>().Where(i => Convert.ToDateTime(i["Data nascimento"]).Month.Equals(mes)).OrderBy(i => Convert.ToDateTime(i["Data nascimento"]).Day).ToList();
                    writer.Write("<div id=\"eventoMes" + mes + "\" style=\"display:none\">");
                    if (resultUsuario.Count > 0)
                    {
                        writer.Write("<br>");
                        foreach (SPListItem itemUsuario in resultUsuario)
                        {
                            if (!itemUsuario["Nome"].ToString().Contains("FAX"))
                            {
                                writer.Write("<ul>");
                                writer.Write("<li><strong>" + Convert.ToDateTime(itemUsuario["Data nascimento"]).Day.ToString() + " de " + mesExtension.TextInfo.ToTitleCase(dtif.GetMonthName(Convert.ToInt32(mes))) + "</strong>");
                                writer.Write("<span><a href=\"/Paginas/usuario.aspx?ID_Lista=" + itemUsuario["ID"] + "&strLetra=" + itemUsuario["Nome"].ToString().Substring(0, 1) + "\">" + itemUsuario["Nome"].ToString().ToUpper() + "</a></span>");
                                writer.Write("</li></ul>");
                            }
                        }
                    }
                    else
                    {
                        writer.Write("<br>");
                        writer.Write("<p>Nenhum registro encontrado.</p>");
                    }
                    writer.Write("</div>");
                }

            }
            catch (Exception excep)
            {
                writer.Write("<br />");
                writer.Write("<p>Não foi possivel conectar ao servidor.</p>");
            }
        }
    }

}
